<?php

//include '../include/init.php';

class image
{
	/*
	根据开始坐标和结束坐标切割图片
	参数：图片地址 开始xy,结束xy
	*/
	static function cut_img($imgurl,$sx,$sy,$width,$height,$name)
	{
		$info=self::getinfo($imgurl);

		$cropped_image=imagecreatetruecolor($width,$height);
		$createname='imagecreatefrom'.$info['func'];
		
		imagecopy($cropped_image,$createname($imgurl),0,0, $sx, $sy, $width, $height);
		!$name?$name=ROOT.'upload/temp/img/cut/imgcut_'.time().self::getcode(8).'_'.round($width).'x'.round($height).'.'.$info['func']:'';
		//var_dump($name);
		
		$ifunc='image'.$info['func'];
		if(!$ifunc($cropped_image,$name))
		{
			return false;
		}
		else
		{
			return $name;
		}

	}

	/*
	旋转图片
	*/
	static function rotate($imgurl,$degrees,$iscover=false)
	{
		$info=self::getinfo($imgurl);
		$createname='imagecreatefrom'.$info['func'];
		$rotate=imagerotate($createname($imgurl), $degrees,0);
		$ifunc='image'.$info['func'];

		if($iscover)
		{
			if(!$ifunc($rotate,$imgurl))
			{
				return false;
			}
			else
			{
				return $imgurl;
			}
		}
		else
		{
			$name=explode('.',$imgurl);
			$name=str_replace('.'.$name[count($name)-1],'_rotated.'.$name[count($name)-1],$imgurl);
			if(!$ifunc($rotate,$name))
			{
				return false;
			}
			else
			{
				return $name;
			}
		}
	}


	/*
		滤镜混合函数
		filtermix(图片地址,滤镜类型[图片、效果],滤镜地址[图片地址,或效果类型],滤镜透明度)
	*/
	static function filtermix($imgurl,$filtertype,$filterimgurl_mixtype,$filteropacity)
	{

	}


	/*
	参数:图片地址
	将图片处理成黑白状态
	并返回生成的黑白图片地址
	*/
	static function setgray($imgurl)
	{
		$info=self::getinfo($imgurl);
		//$imgname=explode(".",basename($imgurl));
		$createfrom='imagecreatefrom'.$info['func'];
		$im = $createfrom($imgurl);
		if ($im && imagefilter($im, IMG_FILTER_GRAYSCALE)) {
			$newdir=str_replace('.','_gray.',$imgurl);
			$image='image'.$info['func'];
			$image($im, $newdir);
			return $newdir;
		} else {
			return false;
		}
		//header('content-type:image/jpeg');
		//imagejpeg($im);
		//imagedestroy($im);
	}

	/*
	 * 生成缩略图的静态方法
	 */
	static function make_thumb($dir,$w,$h)
	{
		//使用大图做画布
		$info=self::getinfo($dir);

		$func='imagecreatefrom'.$info['func'];
		$big=$func($dir);
		//使用小图做画布
		$small=imagecreatetruecolor($w, $h);
		$color=imagecolorallocate($small, 0, 0, 0);
		imagefill($small, 0, 0, $color);
		//大图缩略到小图中
		//设置最小比例
		$scale=min($w/$info['width'],$h/$info['height']);
		$sx=$info['width']*$scale;
		$sy=$info['height']*$scale;
		$dst_x=($w-$sx)/2;
		$dst_y=($h-$sy)/2;
		imagecopyresampled($small, $big, $dst_x, $dst_y, 0,0,$sx, $sy, $info['width'], $info['height']);
		//输出小图
		$thumb=str_replace('.', '_thumb_'.$w.'x'.$h.'.', $dir);
		$ifunc='image'.$info['func'];
		if(!$ifunc($small,$thumb))
		{
			return false;
		}
		//return str_replace(ROOT, '', $thumb);
		return $thumb;
	}
	

	/*
	参数:图片地址
	获取图片信息
	返回图片宽高，格式(用于调用图片方法)
	*/
	static function getinfo($dir)
	{
		$info=array();
		if(!$arr=getimagesize($dir))
		{
			return false;
		}
		$info['width']=$arr[0];
		$info['height']=$arr[1];
		switch ($arr[2]) {
			case '1':
				$info['func']='gif';
				break;
			case '2':
				$info['func']='jpeg';
				break;
			case '3':
				$info['func']='png';
				break;
			case '6':
				$info['func']='wbmp';
				break;
			default:
				$info['func']=false;
			break;
		}
		return $info; 
	}



	/*
	 * 验证码2个静态方法
	 */
	static function getcode($n='6')
	{
		$code='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$chkcode=substr(str_shuffle($code), 0,$n);
		session_start();
		$_SESSION['valcode']=strtolower($chkcode);
		return $chkcode;
	}
	
	static function chk_code($w,$h)
	{
		$code=self::getcode(4);
		//创建画布
		$im=imagecreatetruecolor($w,$h);
		$niu=imagecreatetruecolor($w,$h);
		//调节颜色
		$back=imagecolorallocate($im, rand(50, 200), rand(50, 200), rand(50, 200));
		//填充画布
		imagefill($im, 0, 0, $back);
		imagefill($niu, 0, 0, $back);
		
		$str=imagecolorallocate($im, rand(50, 150), rand(50, 150), rand(50, 150));
		//创建字符串
		$dx=round(($w-80)/2);
		$dy=round(($h-16)/2);
		imagestring($im, 4, $dx, $dy, $code, $str);
		//$fontsize=round($w/5);
		//imagettftext($im,10, 0, 0, 0, $str, ROOT."tools/TT0131M_.TTF", $code);
		
		//设置干扰线
		$c=imagecolorallocate($im, rand(50, 200), rand(50, 200), rand(50, 200));
		imageline($im, rand(0, 60), rand(0, 60), rand(0, 60), rand(0, 60), $c);
		$k=imagecolorallocate($im, rand(50, 200), rand(50, 200), rand(50, 200));
		imageline($im, rand(0, 60), rand(0, 60), rand(0, 60), rand(0, 60), $k);
		
		/*验证码扭曲
		 * 1,创建一模一样大画布
		 * 2，遍历原图像素，以像素单位正弦角度粘贴到画布上
		 */
		$bodong=4;
		$pianyi=360;
		for($i=1;$i<=60;$i++)
		{
			//按正弦函数计算Y轴波动
			$y=sin(deg2rad(($pianyi/$w)*$i))*$bodong;
			imagecopy($niu, $im, $i, $y, $i, 0, 1, $h);
		}
		
		//生成验证码
		//打印到网页上
		header('content-type:image/jpeg');
		imagejpeg($niu);
	}








}

//echo image::make_thumb(image::setgray(ROOT.'img/userhead5_80.jpg'),80,80);
